package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.vo.CategoryView;
import com.atguigu.gmall.product.entity.SkuImage;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.*;
import com.atguigu.gmall.product.vo.SkuInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/inner/rpc/product")
public class  SkuRpcController {

    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    SkuImageService skuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;
    @Autowired
    BaseCategory1Service baseCategory1Service;
    @Autowired
    SpuInfoService spuInfoService;

    //查询sku_info
    @GetMapping("/skuinfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId")Long skuId){
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return Result.ok(skuInfo);
    }
    //查询所有图片
    @GetMapping("/skuinfo/images/{skuId}")
    public Result<List<SkuImage>> getSkuImages(@PathVariable("skuId")Long skuId){
        QueryWrapper<SkuImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id",skuId);
        List<SkuImage> skuImages = skuImageService.list(queryWrapper);
        return Result.ok(skuImages);
    }
    //查询sku对应的spu定义的所有销售属性名和值
    @GetMapping("/skuinfo/spu/saleattrandvalue/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrAndValue(@PathVariable("spuId")Long spuId,
                                                            @RequestParam("skuId")Long skuId){
        List<SpuSaleAttr> saleAttrs = spuSaleAttrService.getSpuSaleAttrAndValue(spuId,skuId);
        return Result.ok(saleAttrs);
    }
    //查询某个sku对应分类的完整路径
    @GetMapping("/skuinfo/categoryview/{c3Id}")
    public Result<CategoryView> getSkuCategoryView(@PathVariable("c3Id")Long c3Id){
        CategoryView categoryView = baseCategory1Service.getSkuCategoryView(c3Id);
        return Result.ok(categoryView);
    }
    //查询某个sku对应的spu定义的所有sku的销售属性值组合
    @GetMapping("/skuinfo/valuejson/{spuId}")
    public Result<String> getSkuValueJson(@PathVariable("spuId")Long spuId){
        String json = spuSaleAttrService.getSkuValueJson(spuId);
        return Result.ok(json);
    }
    //商品详情页价格
    @GetMapping("/skuinfo/price/{skuId}")
    public Result<BigDecimal> getSkuPrice(@PathVariable("skuId")Long skuId){
        BigDecimal price = skuInfoService.getSkuPrice(skuId);
        return Result.ok(price);
    }
}
